import {
  Body,
  Controller,
  Post,
  UseGuards,
  Request,
  Get,
  Req,
  BadRequestException,
} from '@nestjs/common'
import { AuthGuard } from '@nestjs/passport'
import { CreateTaskDto } from 'src/database/task/dto/create-task.dto'
import { NormalService } from './normal.service'

@Controller('normal')
@UseGuards(AuthGuard('jwt'))
export class NormalController {
  constructor(private readonly normalService: NormalService) { }

  /* 
      * 创建任务
          task：一个对象，包含CreateTaskDto内所有属性，除了uid
      */

  @Post('createTask')
  async createTask(@Body() task: Partial<CreateTaskDto>, @Request() req) {
    // 获取用户的id
    const uid = req.user.uid
    // 创建任务并返回结果
    return await this.normalService.createTask(task, uid)
  }

  /* 
      获取所有任务
      */
  @Get('getAllTasks')
  async getAllTask(@Request() req) {
    // 获取用户的id
    const uid = req.user.uid
    const res = await this.normalService.findTask(uid)
    if (!res) {
      throw new BadRequestException('未找到数据')
    }
    return res
  }

  /* 
          完成任务
      */
  @Post('finishTask')
  async finishTask(@Body() body, @Request() req) {
    // 任务id
    const { tid } = body
    const res = await this.normalService.finishTask(tid)
    if (!res) {
      throw new BadRequestException('未找到数据')
    }
    return res
  }

  /* 
      删除任务
          tid：待删除的任务id
      */
  @Post('delTask')
  delTask(@Body() body) {
    // 任务id
    const tid = body.tid
    return this.normalService.removeTask(tid)
  }

  // 更新任务等级
  @Post('updateTaskLevel')
  updateTaskLevel(@Body() body) {
    const { tid, level } = body
    return this.normalService.updateTaskLevel(tid, level)
  }


}
